define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Auction, $stateParams, $window, $timeout, $ionicSlideBoxDelegate, GoPage, GoAddress, GoPicture, GoDialog, $ionicModal,GoCatTypeAuth) {
		var region_info = new Array();
		var store_info = new Array();
		$scope.scope = new Object();
		$scope.input = new Object();
		$scope.input.act_id = $stateParams.act_id;
		$scope.scope.id1 = null;
		$scope.scope.id2 = null;
		$scope.scope.id3 = null;
		$scope.cat_id = null;
		$scope.isLoad = false; //可以加载
		$scope.datas = new Object();
		$scope.is_goods = false;
		$scope.cat_type = new Array();
		$scope.input.key = null;
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.input.region = null;
		$scope.input.cat_type = null;
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
		$scope.input.name = null;
		$scope.input.brand = null;
		$scope.input.agent_count = 0;
		$scope.input.goods_count = 0;
		$scope.is_same = false;
		$scope.goods_is_same = false;
		$scope.province_name = null;
		$scope.city_name = null;
		$scope.districe_name = null;
		$scope.region_name = null;
		$scope.scope.image = new Array();
		$scope.scope.create_group = false;
		$scope.scope.has_packets = false;
		$scope.province_name = null;
		$scope.city_name = null;
		$scope.districe_name = null;
		$scope.region_name = null;
		$scope.scope.create_group = false;
		$scope.scope.has_packets = false;
		$scope.scope.cat_type = new Array();
		$scope.datas = new Object();
		$scope.cat_id = null;
		$scope.cat_name = null;
		var store = new Array();
		$scope.store = new Array();
		$scope.scope.flg = false;
		var x = 0;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		/*//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};*/

		//选取商品
		$scope.onshow_searchpro = function() {
			GoPage.go_states("searchpro_common");
		} //图片移除
		$scope.ondelete = function(index) {
			$scope.scope.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}
		$scope.onupload = function(val) {
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function(e) {
					//图片进行压缩处理
					GoPicture.compress(file,e.target.result, 50, function(base64) {
						$scope.$apply(function() {
							var i = $scope.scope.image.length;
							$scope.scope.image[i] = {
								"id": null,
								"image": base64
							}
							val.value = "";
						});
					});
				}
			}
			//上传成功
			val.value = null;
		};
		$scope.add_type = function() {
			if($scope.input.cat1 != undefined && $scope.input.cat1 != null) {
				$scope.cat_id = $scope.input.cat1;
				//等有了品类联动接口再替换成name
				$scope.cat_name = $scope.input.cat1.name;
				if($scope.input.cat2 != undefined && $scope.input.cat2 != null) {
					$scope.cat_id = $scope.input.cat1 + ',' + $scope.input.cat2;
					$scope.cat_name = $scope.input.cat1.name + '-' + $scope.input.cat2.name;
					if($scope.input.cat3 != undefined && $scope.input.cat3 != null) {
						$scope.cat_id = $scope.input.cat1 + ',' + $scope.input.cat2 + ',' + $scope.input.cat3;
						$scope.cat_name = $scope.input.cat1.name + '-' + $scope.input.cat2.name + '-' + $scope.input.cat3.name;
					}
				}
			}
			var n = $scope.scope.cat_type.length;
			if(n == 1) {
				GoDialog.toast("已经选有商品品类");
				return;
			}
			for(var i = 0; i < n; i++) {
				if($scope.scope.cat_type[i].id == $scope.cat_id)
					return;
			}
			//console.log($scope.cat_id);
			if($scope.cat_id != null)
				$scope.scope.cat_type[n] = {
					"id": $scope.cat_id,
					"name": $scope.cat_name
				};
		}
		$scope.delete_type = function(index) {
			$scope.scope.cat_type.splice(index, 1);
		}

		$scope.init_data = function() {
			$scope.bind('input.province','input.city','input.distric');
			//console.log($scope.input.act_id)
			if($scope.input.act_id!=null) {
				Auction.getdetail($scope.input.act_id, function(data) {
				   // console.log(data)
					$scope.scope = data;
				});
				Auction.acution_agenter($scope.input.act_id,function(data) {
					$scope.datas = data;
					//console.log(data);
					if($scope.datas.goods_name != undefined) {
						if($scope.datas.goods_name != null) {
							$scope.is_goods = true;
						}
					}
					for(var i = 0; i < $scope.datas.store.length; i++) {
							$scope.datas.store[i].province = null;
							$scope.datas.store[i].city = null;
							$scope.datas.store[i].district = null;
							//初始化想要绑定的模型
							$scope.bind('datas.store[' + i + '].province', 'datas.store[' + i + '].city', 'datas.store[' + i + '].district', i);
						}
				});
			}
		}
		$scope.listen_province = function(data, id) {
			//动态刷新items，并且防止删除时的回调报错
			if(id != undefined&&$scope.datas.store.length>id) {
				$scope.datas.store[id].city_items = data;
				$scope.datas.store[id].district_items = null;
				$scope.datas.store[id].city = null;
				$scope.datas.store[id].district = null;
			} else {
				$scope.input.city = null;
				$scope.input.district = null;
				$scope.city_items = data;
				$scope.district_items = null;
			}

		};
		$scope.listen_city = function(data, id) {
		if(id != undefined)
				$scope.datas.store[id].district_items = data;
			else
				$scope.input.district = null;
		};
		//动态绑定三级地域的模型
		$scope.bind = function(ob1, ob2, ob3, i) {
			var items = new Array();
			var obj1 = {
				name: ob1,
				id: i
			};
			var obj2 = {
				name: ob2,
				id: i
			};
			var obj3 = {
				name: ob3,
				id: i
			};
			items.push(obj1);
			items.push(obj2);
			items.push(obj3);
			GoAddress.init_load($scope, items);
		}
		$scope.delete_store = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].checked = false;
					if($scope.datas.store[i].goods != undefined)
						for(var n = 0; n < $scope.datas.store[i].goods.length; n++) {
							$scope.datas.store[i].goods[n].checked = false;
						}
					$scope.datas.store.splice(i, 1);
				}
			}
			//console.log($scope.store);
		}
		$scope.region_choose = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					if($scope.datas.store[i].province != null) {
						if($scope.datas.store[i].region == undefined)
							$scope.datas.store[i].region = new Array();
						var n = $scope.datas.store[i].region.length;
						$scope.region_name = $scope.datas.store[i].province.name;
						$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].province.id;
						if($scope.datas.store[i].city != null) {
							$scope.region_name += '/' + $scope.datas.store[i].city.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].city.id;
						}
						if($scope.datas.store[i].district != null) {
							$scope.region_name += '/' + $scope.datas.store[i].district.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].district.id;
						}
						for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
							if($scope.datas.store[i].region[n].region_id == $scope.region_id) {
								GoDialog.toast("该地域已存在");
								return;
							}

						}
						$scope.datas.store[i].region[n] = {
							"region_id": $scope.region_id,
							"region_name": $scope.region_name

						}
						return;
					}

				}
			}
		}
		$scope.region_delete = function(store_id, index) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
							$scope.datas.store[i].region.splice(index, 1);
							return;
				}
			}
		}

		$ionicModal.fromTemplateUrl('templates/modal/searchpro_common.html', {
			scope: $scope,
		//			animation: 'slide-in-up'
		}).then(function(modal) {
			$scope.goodsModal = modal;
		});
		$scope.openGoods = function() {
			$scope.goodsModal.show();
		};
		$scope.closeGoods = function() {
			for(var i = 0; i < $scope.goods_data.goods.length; i++) {
				if($scope.goods_data.goods[i].checked == true) {
					$scope.datas.goods_id = $scope.goods_data.goods[i].goods_id;
					$scope.datas.goods_name = $scope.goods_data.goods[i].name;
					$scope.datas.image = $scope.goods_data.goods[i].image.image;
					$scope.datas.image_id = $scope.goods_data.goods[i].image.id;
					$scope.is_goods = true;
					break;
				}
			}
			$scope.goodsModal.hide();
		};

		$scope.delete_goods = function() {
			$scope.datas.goods_id = null;
			$scope.datas.name = null;
			$scope.datas.image = null;
			$scope.datas.id = null;
			$scope.is_goods = false;
		}
		$ionicModal.fromTemplateUrl('templates/modal/searchpro_agent.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.specModal = modal;
		});
		$scope.openSpec = function() {
			$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.input.province_name = null; //省市区三级联动
		$scope.input.city_name = null;
		$scope.input.district_name = null;
		$scope.input.agent_count=0;
		$scope.agenter_list = new Object();
		$scope.specModal.show();
		};
		$scope.closeSpec = function() {
			//console.log($scope.agenter_list);
			if($scope.datas.store == undefined)
				$scope.datas.store = new Array();
			if($scope.agenter_list != undefined) {

				for(var i = 0; i < $scope.agenter_list.store.length; i++) {
					//console.log($scope.agenter_list);
					if($scope.agenter_list.store[i].checked == true) {
						$scope.is_same = false;
						//console.log($scope.is_same);
						for(var n = 0; n < $scope.datas.store.length; n++) {
							if($scope.datas.store[n].store_id == $scope.agenter_list.store[i].store_id) {
								$scope.is_same = true;
								break;
							}

						};
						//console.log($scope.is_same);
						if($scope.is_same == false) {
							var n = $scope.datas.store.length;
							$scope.datas.store[n] = {
								"store_id": $scope.agenter_list.store[i].store_id,
								"store_name": $scope.agenter_list.store[i].store_name,
								"change_price": false,
								"province":null,
								"city":null,
								"margin_price":null,
								"start_price":null,
								"add_price":null,
								"floor_price":null,
								"count":null

							};
							$scope.bind('datas.store['+n+'].province','datas.store['+n+'].city','datas.store['+n+'].district',n);
						}

					}
				}
			}
			$scope.specModal.hide();
		};
		$scope.doSelectbrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("品牌",$scope.brand_items,$scope.input.brand,function(data){
				$scope.input.brand = data;
				$scope.input.brand_id = data.id;
			});
		}
		$scope.doSelectProvince = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,store.province,function(data){
				store.province = data;
			});
		}
		$scope.doSelectCity = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,store.city,function(data){
				store.city = data;
			});
		}
		$scope.doSelectDistrict = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,store.district,function(data){
				store.district = data;
			});
		}
		//当我们用到模型时，清除它！
		$scope.$on('$destroy', function() {
			if($scope.regionModal)
				$scope.regionModal.remove();
			if($scope.goodsModal)
				$scope.goodsModal.remove();
		});
		//点击其他区域关闭弹出窗
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {
			if(event.target.nodeName === 'ION-CONTENT') {
				if($scope.goodsModal) {
					$scope.goodsModal.hide();
				}
				if($scope.specModal) {
					$scope.specModal.hide();
				}
			}
		});
		$scope.do_agentSearch = function() {
			if($scope.input.province != null){
				$scope.input.region = $scope.input.province.id;
			}
			if($scope.input.city != null){
				$scope.input.region += ','+$scope.input.city.id;
			}
			if($scope.input.district != null){
				$scope.input.region += ','+$scope.input.district.id;
			}
			if($scope.input.key != null || $scope.input.region != null) {
				Auction.agenter_getlist($scope.input.key, $scope.input.region, function(data) {
					$scope.agenter_list = data;
					$scope.input.agent_count = $scope.agenter_list.page[0].count;
				})
			}

		}
		$scope.agenter_checked = function(store_id) {
			for(var i = 0; i < $scope.agenter_list.store.length; i++) {
				if($scope.agenter_list.store[i].store_id == store_id) {
					if($scope.agenter_list.store[i].checked == true)
						$scope.agenter_list.store[i].checked = false;
					else
						$scope.agenter_list.store[i].checked = true;
				}
			}
		}
		$scope.do_goodsSearch = function() {
			if($scope.input.cat1 != null) {
				$scope.input.cat_type = $scope.input.cat1;
			}
			if($scope.input.cat2 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat2;
			}
			if($scope.input.cat3 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat3;
			}
			if($scope.input.cat_type != null || $scope.input.name != null || $scope.input.brand != null) {
			   // console.log($scope.input.cat_type);
				Auction.goods_list($scope.input.cat_type, $scope.input.name, $scope.input.brand_id, function(data) {
					$scope.goods_data = data;
					$scope.input.goods_count = $scope.goods_data.page[0].count;
				})
			}
		}
		$scope.goods_checked = function(goods_id) {
			for(var i = 0; i < $scope.goods_data.goods.length; i++) {
				if($scope.goods_data.goods[i].goods_id == goods_id) {
					$scope.goods_data.goods[i].checked = true;
				} else
					$scope.goods_data.goods[i].checked = false;

			}
			//console.log($scope.goods_data);
		}
		$scope.get_storeinfo = function() {
			store_info = new Array();
			//console.log($scope.datas.store==undefined);
			if($scope.datas.store ==undefined){
				GoDialog.toast("请选择活动代理商");
				return;
			} else if($scope.datas.store.length<1){
				GoDialog.toast("请选择活动代理商");
				return;
			}
			else{
				for(var i = 0; i < $scope.datas.store.length; i++) {
				region_info = new Array();
				if($scope.datas.store[i].region == undefined) {
					GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的地域信息")
					return;
				}else if($scope.datas.store[i].region.length<1){
					GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的地域信息")
					return;
				}
				if(!$scope.datas.store[i].margin_price ){
					GoDialog.toast("请输入" + $scope.datas.store[i].store_name + "的保证金")
					return;
				}
				if(!$scope.datas.store[i].start_price ){
					GoDialog.toast("请输入" + $scope.datas.store[i].store_name + "的起拍价")
					return;
				}
				if(!$scope.datas.store[i].add_price ){
					GoDialog.toast("请输入" + $scope.datas.store[i].store_name + "的每次加价")
					return;
				}
				if(!$scope.datas.store[i].floor_price){
					GoDialog.toast("请输入" + $scope.datas.store[i].store_name + "的保底价")
					return;
				}
				if(!$scope.datas.store[i].count ){
					GoDialog.toast("请输入" + $scope.datas.store[i].store_name + "的数量")
					return;
				}
				//获取地域信息
				if($scope.datas.store[i].region.length < 1) {
					GoDialog.toast("请选择" + $scope.datas.store[i].store_name + "的地域信息");
					return
				} else {
					for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
						region_info[n] = {
							"region_id": $scope.datas.store[i].region[n].region_id
						}

					}

				}
				if( region_info.length > 0)
					store_info[i] = {
						"store_id": $scope.datas.store[i].store_id,
						"change_price": $scope.datas.store[i].change_price,
						"region": region_info,
						"margin_price":$scope.datas.store[i].margin_price,
						"start_price":$scope.datas.store[i].start_price,
						"add_price":$scope.datas.store[i].add_price,
						"floor_price":$scope.datas.store[i].floor_price,
						"count":$scope.datas.store[i].count

					};
			}
				//console.log(store_info);
//			D_add.set_promotion($scope.act_id, $scope.scope.name, $scope.scope.start_time, $scope.scope.end_time, $scope.scope.create_group, $scope.scope.has_packets, $scope.v_type, $scope.scope.image, $scope.scope.brand_id, $scope.scope.cat_type, $scope.type, $scope.order_type, $scope.total, $scope.price, $scope.percent, store_info, function(data) {
//						if(data.success == true) {
//							console.log('chengong');
//						} else {
//
//						}
//					});
			}


		}
		$scope.rule_choose = function(type) {
			if(type == 1) {
				$scope.p_type = 1;
			} else if(type == 2) {
				$scope.p_type = 2;
			} else if(type == 3) {
				$scope.p_type = 3;
			}
		}
		$scope.rule = function() {

		}
		$scope.get_rule = function() {
			rule = new Array();
			if($scope.p_type == 0) {
				GoDialog.toast("请选择红包类型");
			} else {
				if($scope.scope.type1 == true) {
					x = x + 1;
					if(!$scope.scope.count1) {
						GoDialog.toast("请输入商品数量");
						return;
					} else
						$scope.count = $scope.scope.count1;
					if(!$scope.scope.money1) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money1;
					if(!$scope.scope.packet_num1) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num1;
					if(!$scope.scope.issue_num1) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num1;
					$scope.rule_data(1);

				}
				if($scope.scope.type2 == true) {
					x = x + 1;
					if(!$scope.scope.count2) {
						GoDialog.toast("请输入订单数量");
						return;
					} else
						$scope.count = $scope.scope.count2;
					if(!$scope.scope.money2) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money2;
					if(!$scope.scope.packet_num2) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num2;
					if(!$scope.scope.issue_num2) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num2;
					$scope.rule_data(2);
				}
				if($scope.scope.type3 == true) {
					x = x + 1;
					if(!$scope.scope.count3) {
						GoDialog.toast("请输入成交额");
						return;
					} else
						$scope.count = $scope.scope.count3;
					if(!$scope.scope.money3) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money3;
					if(!$scope.scope.packet_num3) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num3;
					if(!$scope.scope.issue_num3) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num3;
					$scope.rule_data(3);
				}
			}
		}
		$scope.rule_data = function(type) {
			var n = rule.length;
			rule[n] = {
				"type": type,
				"count": $scope.count,
				"packet_num": $scope.packet_num,
				"issue_num": $scope.issue_num
			};
		}
		$scope.packets_edit = function() {
			$scope.get_rule();
			if(rule.length == x && x != 0) {
				Auction.packets_edit($scope.act_id, $scope.scope.flg, $scope.scope.advance, rule, function(data) {
					if(data.success == true) {
						GoDialog.toast("保存成功");

					} else
						GoDialog.toast("保存失败");
				})
			}
			rule = new Array();
			x = 0;
		}
		$scope.submit = function() {
			if($scope.scope.name==undefined){
				GoDialog.toast("请填写活动名称");
					return;
			}
			if($scope.scope.start_time==undefined){
					GoDialog.toast("请填写活动开始时间");
					return;
			}
			if($scope.scope.end_time==undefined){
					GoDialog.toast("请填写活动结束时间");
					return;
			}
			if($scope.scope.cat_type.length<1){
					GoDialog.toast("请选择活动的品类");
					return;
			}
			$scope.get_storeinfo();


		}

		//下拉侦听 4-14
        //$scope.input.store_id = Auth.get_store_id();
		//$scope.input.brand_id=null;
        $scope.listen_brand = function(data, obj) {
            $scope.input.cat1 = null;
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        };
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = null;
        };
        //定义绑定
        var items_cat = new Array();
        var obj_brand = { name: 'input.brand' };
        var obj1_cat = { name: 'input.cat1' };
        var obj2_cat = { name: 'input.cat2' };
        var obj3_cat = { name: 'input.cat3' };
        items_cat.push(obj_brand);
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load($scope, items_cat);
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
	}

	controller.$inject = ['$scope', 'Auction', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', 'GoAddress', 'GoPicture', 'GoDialog', '$ionicModal','GoCatTypeAuth'];

	return app.registerController("auctionctrl",controller);
});